package com.ljc.jsouphttpclient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Date: 2023/11/2
 * @Author: clj
 * @Description: 搭建 ip 代理池
 */
public class CrawlerProxyUtils {
    /**
     * 常用 user agent 列表
     */
    public static List<String> USER_AGENT = new ArrayList<String>(10) {
        {
            add("Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19");
            add("Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
            add("Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
            add("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
            add("Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0");
            add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36");
            add("Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
            add("Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3");
            add("Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3");
        }
    };

    /**
     * 随机获取 user agent
     *
     * @return
     */
    public static String randomUserAgent() {
        Random random = new Random();
        int num = random.nextInt(USER_AGENT.size());
        return USER_AGENT.get(num);
    }

    /**
     * 设置代理ip池
     *
     * @param queue 队列
     * @throws IOException
     */
    public static void proxyIpPool(LinkedBlockingQueue<String> queue) throws IOException {


        // 每次能随机获取一个代理ip
        String proxyUrl = "http://192.168.110.51:5010/all/";

        OkHttpClient client = new OkHttpClient();
        // 创建一个请求
        Request request = new Request.Builder()
                .url(proxyUrl)
                .build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            if (response.isSuccessful()) {
                String body = response.body().string();
                JSONArray jsonArray = JSON.parseArray(body);
                int size = Math.min(100, jsonArray.size());
                for (int i = 0; i < size; i++) {
                    // 将请求结果格式化成json
                    JSONObject data = jsonArray.getJSONObject(i);
                    String proxy = data.getString("proxy");
                    queue.add(proxy);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }



        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(proxyUrl);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity, "utf-8");

            JSONArray jsonArray = JSON.parseArray(body);
            int size = Math.min(100, jsonArray.size());
            for (int i = 0; i < size; i++) {
                // 将请求结果格式化成json
                JSONObject data = jsonArray.getJSONObject(i);
                String proxy = data.getString("proxy");
                queue.add(proxy);
            }
        }
        response.close();
        httpclient.close();
        return;
    }


    /**
     * 随机获取一个代理ip
     *
     * @return
     * @throws IOException
     */
    public static String randomProxyIp() throws IOException {

        // 每次能随机获取一个代理ip
        String proxyUrl = "http://192.168.110.51:5010/get/";

        String proxy = "";

        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(proxyUrl);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity, "utf-8");
            // 将请求结果格式化成json
            JSONObject data = JSON.parseObject(body);
            proxy = data.getString("proxy");
        }
        return proxy;
    }

    public static void main(String[] args) throws IOException {
        CrawlerProxyUtils crawlerProxyUtils = new CrawlerProxyUtils();
        //每次随机获取一个代理ip
        String proxy = randomProxyIp();
        System.out.println("随机代理IP: " + proxy);
        System.out.println("====================================");

        //每次随机获取一个user agent
        String userAgent = randomUserAgent();
        System.out.println("user Agent: " + userAgent);
        System.out.println("====================================");

        //获取代理ip池
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
        proxyIpPool(queue);
        System.out.println("代理IP池: " + queue);
        System.out.println("====================================");

    }
}
